package cn.nexd.app.collector.presenter.fileManager;

import android.content.Context;
import android.os.AsyncTask;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import cn.nexd.app.collector.model.FileManagerModel;
import cn.nexd.utils.framework.tools.Logger;
import cn.nexd.utils.framework.tools.SDCardUtil;

/**
 * Created by wangxu on 16/10/9.
 */

public class CollectFileManagerPresenter implements FileManagerContract.Presenter {


    private static final String TAG = "CollectFileManagerPrese";
    private Context context;
    private FileManagerContract.View iView;

    private String parentFilePath;
    private String collectionPath = SDCardUtil.getSDCardPath() + "/nexd/collect/";

    public CollectFileManagerPresenter(Context context, FileManagerContract.View iView) {
        this.context = context;
        this.iView = iView;
    }

    @Override
    public void init() {

    }

    @Override
    public void loadFile() {
        new LoadCollectFilesThread().execute();
    }

    @Override
    public String loadFileContent(String childFileName) {
        return null;
    }

    @Override
    public List<String> loadChildFilesName(String parentFileName) {

        parentFilePath = collectionPath + "/" + parentFileName;

        File parentFile = new File(parentFilePath);

        Logger.debug(TAG, parentFile.getAbsolutePath());
        File[] childFiles = parentFile.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().contains(".zip");
            }
        });
        List<String> childFilesName = new ArrayList<>();

        if (childFiles != null) {
            Arrays.sort(childFiles, new Comparator<File>() {
                @Override
                public int compare(File o1, File o2) {
                    return (int) (o2.lastModified() - o1.lastModified());
                }
            });
            for (File childFile : childFiles) {
                childFilesName.add(childFile.getName());
            }
        }

        return childFilesName;
    }

    private class LoadCollectFilesThread extends AsyncTask {

        // 加载全部数据, 先全部加载,待日后文件多的时候,在处理。
        List<FileManagerModel> logs;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            logs = new ArrayList<>();
        }

        @Override
        protected Object doInBackground(Object[] params) {
            Logger.debug(TAG, "开始扫描后台 SDCard 文件！");
            File file = new File(collectionPath);
            File[] parentFiles = file.listFiles();
            if (parentFiles != null) {
                Arrays.sort(parentFiles, new Comparator<File>() {
                    @Override
                    public int compare(File o1, File o2) {
                        return (int) (o2.lastModified() - o1.lastModified());
                    }
                });

                for (File file1 : parentFiles) {
                    logs.add(new FileManagerModel(file1.getName()));
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            if (logs == null) return;
            iView.updateList(logs);
        }
    }
}
